<?php

namespace App\Http\Controllers\Activity;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\News;
use Validator;
use App\Services\NewsService;

use App\Services\UserGroupService;
use App\Services\PartnerGroupTypeService;
use App\Services\CityService;
use App\Services\UserService;
use Illuminate\Validation\Rule;
use App\BaseDictionary;
use App\UserGroup;
use App\User;
use App\Lib\Auth\UserProfileHelper;

class NewsController extends Controller
{
    private $newsService = null;

    public function __construct(NewsService $newsService)
    {
        $this->newsService = $newsService;
    }

    public function index()
    {
        return view('operations.news.index');
    }

    public function getNews(Request $request)
    {
        return $this->newsService
            ->getNews(array_merge($request->all()));
    }

    public function create()
    {
        return view('operations.news.create');
    }

    public function saveNews(Request $request)
    {
        $input = $request->all();

        $this->validateWhenSaveNews($input);

        News::create($input);

//        News::create([
//            'title'=>$input['title'],
//            'picture'=>isset($input['picture'])?$input['picture']:'0',
//            'type'=>$input['type'],
//            'description'=>$input['description'],
//            'province_id'=>$input['province_id'],
//            'city_id'=>$input['city_id']
//        ]);

        return response()->json([
            'Success' => true,
            'Message' => '新增新闻成功',
        ]);
    }

    public function edit($newsId)
    {
        $partnerGroup = News::findOrFail($newsId);

        return view('operations.news.edit', ['news' => $partnerGroup]);
    }

    public function saveUpdateNews(Request $request)
    {
        $input = $request->all();
        $this->validateWhenSaveUpdateNews($input);
        $newsUpdate = News::findOrFail($input['id']);
        $newsUpdate->update([
            'title' => $input['title'],
            'picture' => $input['picture'],
            'type' => $input['type'],
            'description' => $input['description'],
            'province_id' => $input['province_id'],
            'city_id' => $input['city_id'],
        ]);
//        $this->newsService-> saveUpdateNews($request->all());
        return response()->json([
            'Success' => true,
            'Message' => '修改新闻成功',
        ]);
    }

    public function delNews(Request $request)
    {
        $input = $request->all();

        $this->validateWhenDeleteDepartment($input);

        $department = News::findOrFail($input['id']);
        $department->delete();

        return response()->json([
            'Success' => true,
            'Message' => '删除新闻成功',
        ]);
    }

    private function validateWhenDeleteDepartment(Array $input)
    {
        return Validator::make($input, [
            'id' => [ 'required']
        ], [
            'id.required' => '必须传入新闻ID',
            'id.exists' => '该新闻ID参数有误，不存在'
        ])->after(function ($validator) use($input) {})->validate();
    }

    private function validateWhenSaveNews(Array $input)
    {
        return Validator::make($input, [
            'title' => [
                'required',
                'max:50'
            ],
            'type' => 'max:100',
            'code' => 'max:200',
            'description' => ['max:8000','required'],
            'province_id' => 'numeric|exists:provinces,id',
            'city_id' => [
                'numeric'
            ]
        ], $this->getValidateMessagesWhenCreateNews())->after(function ($validator) use($input) {})->validate();
    }

    private function getValidateMessagesWhenCreateNews()
    {
        return [
            'title.required' => '新闻标题必须存在',
            'description.required' => '新闻内容必须存在',
            'type.max'  => '长度不可超过:max',
            'code.max'  => '长度不可超过:max',
            'description.max'  => '新闻内容长度不可超过:max',
            'province_id.required' => '省份信息必须填写',
            'province_id.numeric' => '省份ID必须是数字',
            'province_id.exists' => '省份ID不存在',
            'city_id.required' => '城市信息必须填写',
            'city_id.numeric' => '城市ID必须是数字',
            'city_id.in' => '城市ID不属于当前省份',
        ];
    }

    private function validateWhenSaveUpdateNews(Array $input)
    {
        return Validator::make($input, [
            'title' => [
                'required',
                'max:50',
            ],
            'type' => 'max:100',
            'code' => 'max:200',
            'description' => [
                'max:8000',
                'required',
            ],
            'province_id' => 'numeric|exists:provinces,id',
            'city_id' => [
                'numeric'
            ]
        ], $this->getValidateMessagesWhenSaveNews())->after(function ($validator) use($input) {})->validate();
    }

    private function getValidateMessagesWhenSaveNews()
    {
        return [
            'title.required' => '新闻标题必须存在',
            'description.required' => '新闻内容必须存在',
            'title.max'  => '新闻标题长度不可超过:max',
            'province_id.required' => '省份信息必须填写',
            'province_id.numeric' => '省份ID必须是数字',
            'province_id.exists' => '省份ID不存在',
            'city_id.required' => '城市信息必须填写',
            'city_id.numeric' => '城市ID必须是数字',
            'city_id.in' => '城市ID不属于当前省份',
        ];
    }
}
